Repository navigation
fix(ai): label tool errors in privacy mode and redact tool span content when sent - #1283
DanielVisca wants to merge 4 commits into
Conversation
There was a problem hiding this comment.
PostHog Review · Standard · reviewed f26dc95: 1 Must fix, 3 Should fix.
See this review and its findings in PostHog Review. Comment @posthog review --deep for a Deep review. A Deep review takes longer and looks deeper than a Standard review.
SDK performance: posthog-pythonBenchmarks completed. No changes flagged for review. Flags require at least a 5% change in the median and all six pairs agreeing on direction. This is a noise filter, not proof of a performance change or a CI gate. View all results
Values are medians of paired measurements. Not flagged does not mean unchanged. Unavailable means a measurement could not be provided, not zero. Estimated allocation measurements retain their instrumentation uncertainty. RSS measures the whole process, not just SDK allocations; observed peaks are sampled checkpoints. Paired ranges in the diagnostics describe variability, not confidence intervals. Each shard uses its own runner; compare Before PR against With PR within a row, not across shards. Compared 102 metric rows; 102 rows shown in all results. Omitted or shortened data remain in |
There was a problem hiding this comment.
Not approved — this change needs a human reviewer.
Re-adding the stamphog label gives the same result unless the changed files change.
stamphog can't auto-review this pull request. The deny-list gate refused it because it touches references/public_api_snapshot.txt, which matches the public_api category. Because of that match, the tier gate placed the whole PR in the never-auto-approve tier (T2-never). The size gate passed, so the refusal isn't about size.
The author's note already asks the AI observability team to review and own it, so the next step is to get that human review. Any change to the public API snapshot needs a person to confirm it was intended.
Gate mechanics and policy version
| Gate | Result | |
|---|---|---|
| prerequisites | ✓ | all clear |
| deny-list | ✗ | matches: public_api in references/public_api_snapshot.txt |
| every_file_attested | ✓ | not a bot PR |
| size | ✓ | 395L, 5F substantive, 746L/11F incl. docs/generated/snapshots — within ceiling |
| tier | ✗ | T2-never because the deny-list matched public_api |
| stamphog 2.9.1 | .stamphog/policy.yml @ unknown · reviewed head 8f4da4b |
|
No review started. stamphog started a review of this pull request less than 10 minutes ago, and a label can request at most one review per 10 minutes. The |
dustinbyrne
left a comment
There was a problem hiding this comment.
AI-assisted review.
The Claude Agent SDK deferred tool-span tracking (_ToolSpanTracker, subagent matching, aclose()/ResultMessage/disconnect() flushing), LangChain ToolMessage dict metadata preservation, and privacy/media redaction work well — including using the fixed "ToolError" label in privacy mode, which normalizes cleanly in PostHog error ingestion (processAiErrorNormalization) without leaking tool output.
Verified end-to-end delivery over real HTTP transport (/i/v1/ai/events) with sdk-mock-server for both Claude Agent SDK and LangChain CallbackHandler, plus base-vs-head regression checks. Leaving a few inline items before approval — most notably a regression where non-tool LangChain chains and root $ai_trace events returning an error ToolMessage are also marked $ai_is_error: True, a scope gap with PostHogMiddleware vs the changeset, and edge-case handling in _format_tool_error.
Follow-up (non-blocking, out of PR scope):
PostHog/posthog-js(@posthog/ai): For cross-SDK parity,@posthog/aistill omits$ai_erroron Claude Agent SDK tool spans (packages/ai/src/claude-agent-sdk/processor.ts), and lacks$ai_span_type: 'tool', error-ToolMessagedetection, andtool_call_id/name/statuspreservation inLangChainCallbackHandler(packages/ai/src/langchain/callbacks.ts), plus$ai_span_type: 'tool'increatePostHogMiddleware(packages/ai/src/langchain/middleware/index.ts).- Untouched
posthog-pythonfiles:posthog/ai/claude_agent_sdk/formatting.pycurrently omitsidonToolUseBlockin$ai_output_choicesandis_erroronToolResultBlockin$ai_input(both are emitted by@posthog/aiinformatting.ts), andPostHogMiddleware._returned_tool_error(posthog/ai/langchain/middleware.py:328-331) still usesRuntimeError(str(response.content))withoutfinalize_ai_contentmedia redaction and emits"RuntimeError: Tool returned an error"in privacy mode rather than"ToolError". Consider aligning these in follow-up PRs with equivalent regression tests.
A new stamphog review started for this PR — the fresh verdict replaces this approval.
There was a problem hiding this comment.
Not approved — escalated to a human reviewer.
Add the stamphog label to request another review once you have addressed this.
The current code addresses all four inline findings, but the risky telemetry and privacy changes have no qualifying current-head assurance. Request a review from an AI observability teammate before re-requesting; Andrew Maguire or Eli Reisman are suggested reviewers based on familiarity.
- Risky territory without independent assurance: Data ingestion or write paths — posthog/ai/claude_agent_sdk/processor.py and client.py defer telemetry emission until tool results arrive and change the result, error and latency properties sent to PostHog.; Data ingestion or write paths — posthog/ai/langchain/callbacks.py and middleware.py change tool-span classification, error reporting and tool-result metadata sent in AI events.; Auth or security-sensitive surface — posthog/ai/utils.py and claude_agent_sdk/processor.py govern privacy-mode suppression and media redaction for newly captured tool outputs and errors.
Gate mechanics and policy version
| Gate | Result | |
|---|---|---|
| prerequisites | ✓ | all clear |
| deny-list | ✓ | no deny categories matched |
| every_file_attested | ✓ | not a bot PR |
| size | ✓ | 406L, 6F substantive, 815L/12F incl. docs/generated/snapshots — within ceiling |
| tier | ✓ | T1-agent / T1d-complex (815L, 12F, two-areas, feat) |
| stamphog 2.9.1 | .stamphog/policy.yml @ 62dba83 · reviewed head 62dba83 |
|
@PostHog review |
|
@DanielVisca PostHog Review is reviewing this pull request. |
|
@PostHog review |
|
@DanielVisca PostHog Review is reviewing this pull request. |
|
@PostHog review |
|
@DanielVisca PostHog Review is reviewing this pull request. |
…nt when sent Follow-up to #1282, rebuilt on main: - $ai_error comes from _format_tool_error: "ToolError" in privacy mode or for empty content, media redacted, plain string items kept - Claude Agent SDK tool input is redacted when the span is sent, so settings changed while the tool ran apply - the public query() closes the processor stream when the caller stops early, so pending tool spans are sent - PostHogMiddleware tool spans set $ai_span_type "tool" Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
f30cb7b to
bb8bc19
Compare
A new stamphog review started for this PR — the fresh verdict replaces this approval.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@PostHog review |
|
@DanielVisca PostHog Review is reviewing this pull request. |
There was a problem hiding this comment.
PostHog Review · Standard · reviewed b5e57f9: 1 Should fix.
This is review round 3 on this pull request. When findings keep coming back in the same code, the approach is usually the problem. Before the next fix, check whether a different design would remove the whole class of findings.
Tool spans again remove $ai_input_state in privacy mode, including one supplied through posthog_properties. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@PostHog review |
A new stamphog review started for this PR — the fresh verdict replaces this approval.
|
@DanielVisca PostHog Review is reviewing this pull request. |
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
A new stamphog review started for this PR — the fresh verdict replaces this approval.
|
@PostHog review |
|
@DanielVisca PostHog Review is reviewing this pull request. |
There was a problem hiding this comment.
Approved.
Privacy-sensitive telemetry changes have targeted regression tests and STRONG author familiarity, satisfying the policy's assurance path despite no current-head reviews. The earlier actionable review concerns are addressed in the current code; explicitly deferred middleware behavior remains outside this change.
- Author wrote 83% of the modified lines and has 4 merged PRs in these paths (familiarity STRONG).
Gate mechanics and policy version
| Gate | Result | |
|---|---|---|
| prerequisites | ✓ | all clear |
| deny-list | ✓ | no deny categories matched |
| every_file_attested | ✓ | not a bot PR |
| size | ✓ | 127L, 5F substantive, 278L/10F incl. docs/generated/snapshots — within ceiling |
| tier | ✓ | T1-agent / T1c-medium (278L, 10F, two-areas, fix) |
| stamphog 2.9.1 | .stamphog/policy.yml @ d357b90 · reviewed head d357b90 |
💡 Motivation and Context
Follow-up to #1282, which shipped tool outcomes on Claude Agent SDK and LangChain tool spans. This PR started as a parallel version of that change. It is now rebuilt on
mainand keeps only what #1282 doesn't have, mostly fixes from review on this PR:$ai_errorfor tool errors comes from one helper,_format_tool_errorinutils.py:ToolErrorinstead of being left out, so the span still counts in the errors view without sending tool output. Onmainthe Claude Agent SDK omits it and LangChain sendsNone.$ai_output_state. Onmain, a LangChain errorToolMessagewith a data URL or base64 block sends the raw media in$ai_error.None,"",[]) gives theToolErrorlabel instead of an empty string. Plain string items in LangChain list content are kept.query()stream early now closes the processor's stream too, so tool spans still waiting for a result are sent. Before, onlyprocessor.query()did this.PostHogMiddlewaretool spans set$ai_span_type: "tool", likeCallbackHandlertool spans.💚 How did you test it?
uv run pytest posthog/test/ai: 1954 passed, 15 skipped.main's source, the new and changed tests fail: privacy-modeToolError(Claude Agent SDK and LangChain), input redaction after full capture turns off mid-tool, early close through the modulequery(), and the middleware span type. The_format_tool_errortests (media blocks, data URL, empty content, plain strings) can't import onmain.ruff format,ruff check,make public_api_check. The public API is unchanged.📝 Checklist
If releasing new changes
sampo addto generate a changeset file🤖 Agent context
Autonomy: Human-driven (agent-assisted)
Written by Claude Opus 5.5 in Claude Code, directed by Daniel. The branch was replaced by a single commit on
mainafter #1282 merged, so the earlier review threads point at code that has since moved.🤖 Generated with Claude Code